﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//responsável pela conexão com sql
using System.Data.SqlClient;

namespace splashScreen
{
    public partial class Cliente : Form
    {
        SqlConnection conect = new SqlConnection("Data Source=localhost;Initial Catalog=PDSD;Persist Security Info=True;User ID=sa;Password=senha");
        public Cliente()
        {
            InitializeComponent();
        }

        private void Cliente_Load(object sender, EventArgs e)
        {

            // TODO: This line of code loads data into the 'pDSDDataSet.Cliente' table. You can move, or remove it, as needed.
            //this.clienteTableAdapter.Fill(this.pDSDDataSet.Cliente);
            // TODO: This line of code loads data into the 'pDSDDataSet.Cliente' table. You can move, or remove it, as needed.
            //this.clienteTableAdapter.Fill(this.pDSDDataSet.Cliente);
           // atualizarGrid();
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            ServiceReference1.Service1SoapClient x = new ServiceReference1.Service1SoapClient();
            clienteDataGridView.DataSource = x.BuscaCliente(txtPesquisar.Text); 
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                if((bairroTextBox.Text != "") && (cepTextBox.Text != "") 
                    &&(dataNascimentoDateTimePicker.Text != "") && (enderecoTextBox.Text !="") && (nomeTextBox.Text != "") && (numeroTextBox.Text !=""))
                {

                    ServiceReference1.Service1SoapClient x = new ServiceReference1.Service1SoapClient();
                    ServiceReference1.Cliente c = new ServiceReference1.Cliente();

                    c.apelido = apelidoTextBox.Text;
                    c.bairro = bairroTextBox.Text;
                    c.cep = cepTextBox.Text;
                    c.complemento = complementoTextBox.Text;
                    c.dataNascimento = dataNascimentoDateTimePicker.Value.ToString();
                    c.endereco = enderecoTextBox.Text;
                    c.nome = nomeTextBox.Text;
                    c.numero = numeroTextBox.Text;
                    c.sexo = sexoComboBox.SelectedItem.ToString();
                    c.webSite = webSiteTextBox.Text;
                    x.InserirCliente(c);


                    //this.Validate();
                    //this.clienteBindingSource.EndEdit();
                    MessageBox.Show("Cadastro realizado com sucesso");
                    atualizarGrid();
                    //this.clienteTableAdapter.Update(this.pDSDDataSet.Cliente);
                    
                    apelidoTextBox.Text = "";
                    bairroTextBox.Text = "";
                    cepTextBox.Text = "";
                    complementoTextBox.Text = "";
                    enderecoTextBox.Text = "";
                    nomeTextBox.Text = "";
                    numeroTextBox.Text = "";
                    //sexoTextBox.Text = "";
                    webSiteTextBox.Text = "";
                }
                else
                {
                    MessageBox.Show("Todos os campos devem ser preenchidos");
                }
            }
            catch(Exception ex) { 
                MessageBox.Show("Não foi possível cadastrar: " + ex.Message);
                }
            }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            ServiceReference1.Service1SoapClient x = new ServiceReference1.Service1SoapClient();
            ServiceReference1.Cliente c = new ServiceReference1.Cliente();

            MessageBox.Show("id do usuario selecionado: " +clienteDataGridView.CurrentRow.Cells[0].Value.ToString());
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DialogResult resultado = MessageBox.Show("Deseja exluir esse registro?",
                                    "Excluir",
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question);

            if (resultado == DialogResult.Yes)
            {
                ServiceReference1.Service1SoapClient x = new ServiceReference1.Service1SoapClient();
                ServiceReference1.Cliente c = new ServiceReference1.Cliente();

                x.ExcluirCliente(int.Parse(clienteDataGridView.CurrentRow.Cells[0].Value.ToString()));
                MessageBox.Show("Cliente excluído!");
                atualizarGrid();
            }
            else
            {
                atualizarGrid();
            }
        }

        private void atualizarGrid()
        {
            ServiceReference1.Service1SoapClient x = new ServiceReference1.Service1SoapClient();
            clienteDataGridView.DataSource = x.SelecionarClientes();
        }

        private void clienteDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            idTextBox.Text = clienteDataGridView.CurrentRow.Cells[0].Value.ToString();
            apelidoTextBox.Text = clienteDataGridView.CurrentRow.Cells[2].Value.ToString();
            bairroTextBox.Text = clienteDataGridView.CurrentRow.Cells[3].Value.ToString(); ;
            cepTextBox.Text = clienteDataGridView.CurrentRow.Cells[4].Value.ToString();
            complementoTextBox.Text = clienteDataGridView.CurrentRow.Cells[5].Value.ToString(); ;
            //dataNascimentoDateTimePicker = clienteDataGridView.CurrentRow.Cells[6].Value.ToString(); ;
            enderecoTextBox.Text = clienteDataGridView.CurrentRow.Cells[7].Value.ToString(); ;
            nomeTextBox.Text = clienteDataGridView.CurrentRow.Cells[1].Value.ToString();
            numeroTextBox.Text = clienteDataGridView.CurrentRow.Cells[8].Value.ToString();
            //sexoTextBox.Text = clienteDataGridView.CurrentRow.Cells[9].Value.ToString();
            webSiteTextBox.Text = clienteDataGridView.CurrentRow.Cells[10].Value.ToString();
        }

        private void numeroTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || //Letras
           char.IsSymbol(e.KeyChar) || //Símbolos
           char.IsWhiteSpace(e.KeyChar) || //Espaço
           char.IsPunctuation(e.KeyChar)) //Pontuação
                e.Handled = true; //Não permitir
        }

        private void cepTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            string apelido, bairro, cep, complemento, dataNascimento, endereco, nome, numero, sexo, webSite;
            int id;
            try
            {
                if ((bairroTextBox.Text != "") && (cepTextBox.Text != "")
                    && (dataNascimentoDateTimePicker.Text != "") && (enderecoTextBox.Text != "") && (nomeTextBox.Text != "") && (numeroTextBox.Text != ""))
                {

                    ServiceReference1.Service1SoapClient x = new ServiceReference1.Service1SoapClient();
                    ServiceReference1.Cliente c = new ServiceReference1.Cliente();

                    id = Convert.ToInt32(idTextBox.Text);
                    apelido = apelidoTextBox.Text;
                    bairro = bairroTextBox.Text;
                    cep = cepTextBox.Text;
                    complemento = complementoTextBox.Text;
                    dataNascimento = dataNascimentoDateTimePicker.Value.ToString();
                    endereco = enderecoTextBox.Text;
                    nome = nomeTextBox.Text;
                    numero = numeroTextBox.Text;
                    sexo = sexoComboBox.SelectedItem.ToString();
                    webSite = webSiteTextBox.Text;
                    x.AtualizarCliente(id,apelido, bairro, cep, complemento, dataNascimento, endereco, nome, numero, sexo, webSite);


                    //this.Validate();
                    //this.clienteBindingSource.EndEdit();
                    MessageBox.Show("Edição realizada com sucesso");
                    atualizarGrid();
                    //this.clienteTableAdapter.Update(this.pDSDDataSet.Cliente);

                    apelidoTextBox.Text = "";
                    bairroTextBox.Text = "";
                    cepTextBox.Text = "";
                    complementoTextBox.Text = "";
                    enderecoTextBox.Text = "";
                    nomeTextBox.Text = "";
                    numeroTextBox.Text = "";
                    //sexoTextBox.Text = "";
                    webSiteTextBox.Text = "";
                }
                else
                {
                    MessageBox.Show("Todos os campos devem ser preenchidos");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Não foi possível Editar: " + ex.Message);
            }
           
        }

      
    }
}

